package com.hzj.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzj.exception.HospitalException;
import com.hzj.helper.HttpRequestHelper;
import com.hzj.pojo.*;
import com.hzj.service.DepartmentService;
import com.hzj.service.HospitalInfoService;
import com.hzj.service.HospitalSetService;
import com.hzj.service.ScheduleService;
import com.hzj.utils.MD5;
import com.hzj.utils.Result;
import com.hzj.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiContriller {
    @Autowired
    HospitalInfoService his;
    @Autowired
    HospitalSetService hss;
    @Autowired
    DepartmentService ds;
    @Autowired
    ScheduleService ss;

    //    上传医院
    @PostMapping("/saveHospital")

    public Result saveHospital(HttpServletRequest request) {
        //验证签名
        Map<String, Object> paramMap = verification(request);
        //将map转换为对象
        paramMap.remove("timestamp");
        String bookingRule = (String) paramMap.get("bookingRule");
        paramMap.remove("bookingRule");
        Bookingrule bookingrule = JSONObject.parseObject(bookingRule, Bookingrule.class);


        String jsonString = JSONObject.toJSONString(paramMap);
        System.out.println("=>>>>>>>>>>>>|" + jsonString);
        HospitalInfo hospitalInfo = JSONObject.parseObject(jsonString, HospitalInfo.class);
        //存取对象
        boolean save = his.updateAndSave(hospitalInfo, bookingrule);

        return Result.ok();
    }


    //查询医院
    @PostMapping("/hospital/show")
    public Result show(HttpServletRequest request) {
        //验证签名
        Map<String, Object> paramMap = verification(request);
        String hoscode = (String) paramMap.get("hoscode");
        System.out.println("---------------查询医院");
        //查询数据库
        HospitalInfo one = his.checkByHoscode(hoscode);

        return Result.ok(one);
    }

    //查询科室
    @PostMapping("department/list")
    public Result departmentList(HttpServletRequest request){
        //验证签名
        Map<String, Object> paramMap = verification(request);
        //当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        IPage iPage=new Page(page,limit);
        QueryWrapper<Department> queryWrapper=new QueryWrapper<Department>();
        queryWrapper.select("id","hoscode","depcode","depname","bigcode","bigname","intro").groupBy("bigname","depname");
        IPage iPage1= ds.page(iPage,queryWrapper);
        return Result.ok(iPage1);
    }


    //上传科室
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //验证签名
        Map<String, Object> paramMap = verification(request);
        paramMap.remove("timestamp");
        String s = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(s, Department.class);


        boolean b = ds.updateAndSave(department);
        if (!b) {
            throw new HospitalException(ResultCodeEnum.FAIL);
        }
        return Result.ok();
    }

    //删除科室
    @PostMapping("/department/remove")
    public Result delDept(HttpServletRequest request) {
        //验证签名
        Map<String, Object> paramMap = verification(request);
        //获取参数
        String depcode = (String) paramMap.get("depcode");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("depcode", depcode);
        boolean remove = ds.remove(wrapper);
        if (!remove) {
            throw new HospitalException(ResultCodeEnum.FAIL);
        }
        return Result.ok();
    }

    //上传排班
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> map = verification(request);
        map.remove("timestamp");
        String s = JSONObject.toJSONString(map);
        Schedule schedule = JSONObject.parseObject(s, Schedule.class);

        boolean b = ss.updateAndSave(schedule);
        if (!b) {
            throw new HospitalException(ResultCodeEnum.FAIL);
        }
        return Result.ok();
    }




    //查询排班接口
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        //验证签名
        Map<String, Object> paramMap = verification(request);

//        //科室编号
//        String depcode = (String) paramMap.get("depcode");

        //医院唯一标识
        String hoscode = (String) paramMap.get("hoscode");
        //
        //当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        IPage iPage = new Page(page, limit);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("hoscode", hoscode);
        IPage iPage1 = ss.page(iPage, wrapper);

        return Result.ok(iPage1);
    }


    //删除排班
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request) {
        //验证签名
        Map<String, Object> paramMap = verification(request);
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("hosScheduleId", hosScheduleId);
        boolean remove = ss.remove(wrapper);
        if (!remove) {
            throw new HospitalException(ResultCodeEnum.FAIL);
        }

        return Result.ok();
    }


    public Map<String, Object> verification(HttpServletRequest request) {
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        System.out.println("----------------" + paramMap);
        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");


        System.out.println("----------------2" + paramMap);
        //2 根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("hoscode", hoscode);
        //查询数据库
        HospitalSet one = hss.getOne(wrapper);
        System.out.println("------------>" + one);
        if (one == null) {
            throw new HospitalException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        //验证签名
        if (HttpRequestHelper.isSignEquals(paramMap, hospSign)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        return paramMap;
    }


}
